//
//  AppDelegate.m
//  View_1
//
//  Created by 愤怒的振振 on 15-4-15.
//  Copyright (c) 2015年 LiuWeiZhen. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()
{
    UIView *_redView;
    UIView *_greenView;
    UIView *_blueView;
}
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // [self test1]; // 交换两个view的frame有可能导致看不见的问题
    
    // 展示alpha, 及动画效果
    // [self test2];
    
    [self setRootController];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)setRootController {
    RootViewController *controller = [[RootViewController alloc] init];
    self.window.rootViewController = controller;
    [controller release];
}

- (void)test2 {
    UIView *purpleView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 200)];
    [self.window addSubview:purpleView];
    purpleView.tag = 100;
    purpleView.backgroundColor = [UIColor purpleColor];
    [purpleView release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"测试" forState:UIControlStateNormal];
    button.frame = CGRectMake(20, CGRectGetMaxY(purpleView.frame), self.window.frame.size.width - 40, 40);
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];
}

- (void)buttonClick:(UIButton *)button {
    UIView *purpleView = (UIView *)[self.window viewWithTag:100];
    CGRect targetFrame = CGRectMake(20, 20, 10, 10);
    // purpleView.alpha = 0.5;
    
    // 第一个参数：动画时间，以秒为单位
    // 第二个参数：是一个block, 在里面对frame等变化操作，UIView自己会根据这个block里面的内容的变化自动动画
    // 第三个参数：当动画完成后回调
    [UIView animateWithDuration:2 animations:^{
        purpleView.alpha = 0.5;
        purpleView.frame = targetFrame;
    } completion:^(BOOL finished) {
        purpleView.alpha = 1.0;
        purpleView.frame = CGRectMake(20, 20, 100, 100);
    }];
}

- (void)test1 {
    // 新建三个view, 红，绿，蓝
    // 这三个view全都加在self.view上面，层次结构为：先加红，再加绿，再加蓝
    CGRect frame = CGRectMake(20, 30, self.window.frame.size.width - 40, 60);
    _redView = [[UIView alloc] initWithFrame:frame];
    _redView.backgroundColor = [UIColor redColor];
    [self.window addSubview:_redView];
    
    frame.origin.y = CGRectGetMaxY(frame) + 30;
    _greenView = [[UIView alloc] initWithFrame:frame];
    _greenView.backgroundColor = [UIColor greenColor];
    [self.window addSubview:_greenView];
    
    // frame.origin.y = CGRectGetMaxY(frame) + 30;
    _blueView = [[UIView alloc] initWithFrame:frame];
    _blueView.backgroundColor = [UIColor blueColor];
    [self.window addSubview:_blueView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    frame.origin.y = CGRectGetMaxY(frame) + 20;
    button.frame = frame;
    [button setTitle:@"Change Frame" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(changeFrame:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];
}

- (void)changeFrame:(id)sender {
    CGRect redFrame = _redView.frame;
    _redView.frame  = _blueView.frame;
    _blueView.frame = redFrame;
    //[self.window bringSubviewToFront:_redView];
    
    // _redView.hidden = YES;
    // _redView.alpha = 0.5; // 设置透明度
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - dealloc
- (void)dealloc {
    self.window = nil;
    [_redView release];
    [_greenView release];
    [_blueView release];
    [super dealloc];
}

@end
